package com.lancoo.znbkxx.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.facebook.drawee.view.SimpleDraweeView;
import com.lancoo.znbkxx.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by TangJun on 2019/2/22
 */
public class EditImageAdapter extends  RecyclerView.Adapter<EditImageAdapter.ViewHolder>{

    private List<String> mData;
    private int state = 0;
    private final int NOMAL_TYPE = 0;
    private final int FOOT_TYPE = 1;
    private OnDeleteClickLister mDeleteClickListener;
    private String url = "http://172.16.41.235:8231/";
    public EditImageAdapter(List<String> data,OnItemClickListener onItemClickListener) {
        List<String> path = new ArrayList<>();
        for (String string : data){
            path.add(url + string);
        }
        this.mData = path;
        this.onItemClickListener = onItemClickListener;
            }

    public void updateData(int state) {
       /* List<String> path = new ArrayList<>();
        for (String string : data){
            path.add(url + string);
        }*/
        this.state = state;
        //this.mData = data;
        notifyDataSetChanged();
}
    public void addNewItem(String path) {
        if(mData == null) {
            mData = new ArrayList<>();
        }
        mData.add(getItemCount() - 1, path);
        notifyItemInserted(getItemCount());
    }
    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1){
            return FOOT_TYPE;
        }else {
            return NOMAL_TYPE;
        }

    }

    /**
     * 删除Item
     */
    public void deleteItem(int position) {
        if(mData == null || mData.isEmpty()) {
            return;
        }
        mData.remove(position);
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
            View nomal = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_edit_image, viewGroup, false);
            // 实例化viewholder
            View foot = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_edit_image, viewGroup, false);
            if (viewType == FOOT_TYPE){
                return new ViewHolder(foot);
            }else {
                return new ViewHolder(nomal);
            }

            }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        if (getItemViewType(i) == NOMAL_TYPE){
            viewHolder.ivAdd.setImageURI(mData.get(i));
            if (state == 0){
                viewHolder.ivDelete.setVisibility(View.INVISIBLE);
            }else {
                viewHolder.ivDelete.setVisibility(View.VISIBLE);
            }
            viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDeleteClickListener != null) {
                        mDeleteClickListener.onDeleteClick(v, i);
                    }
                }
            });
           /* viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    deleteItem(i);
                }
            });*/
        }else {
            viewHolder.ivAdd.setImageResource(R.mipmap.icon_add);
            if (state == 0){
                viewHolder.ivAdd.setVisibility(View.INVISIBLE);
            }else {
                viewHolder.ivAdd.setVisibility(View.VISIBLE);
            }

            viewHolder.ivDelete.setVisibility(View.INVISIBLE);
            viewHolder.ivAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener != null) {
                        onItemClickListener.onItemClick(v,i);
                    }
                }
            });
        }

            }

    @Override
    public int getItemCount() {
            return mData == null ? 0 : mData.size() + 1 ;
            }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView ivAdd;
        ImageView ivDelete;
        public ViewHolder(View itemView) {
            super(itemView);
            ivAdd = itemView.findViewById(R.id.iv_add);
            ivDelete = itemView.findViewById(R.id.iv_delete);
        }
}public void setOnDeleteClickListener(OnDeleteClickLister listener) {
        this.mDeleteClickListener = listener;
    }
    public interface OnDeleteClickLister {
        void onDeleteClick(View view, int position);
    }
private OnItemClickListener onItemClickListener;
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }}

